#ifndef							_RENDERER_H_
#define							_RENDERER_H_

#include <string>
#include <unordered_map>
#include "Irrlicht/irrlicht.h"
#include "Common/Common.h"
#include "CGUICodeEditor.h"
#include "SentimentBar.h"

#define		DEFAULT_PLANE_TEXTURE			"./media/plane_texture.jpg"
#define		DEFAULT_SKYDOME_TEXTURE			"./media/skydome.jpg"
#define		DEFAULT_BOX_MESH				"./mesh/box_unit.3ds"
#define		DEFAULT_SPHERE_MESH				"./mesh/sphere_unit.3ds"
#define		DEFAULT_CYLINDER_MESH			"./mesh/cylinder_unit.3ds"

class CRatingGUI;
class CEmotionBar;
class CAvgEmoGraph;

class CRenderer
{
public:
	~CRenderer();

	int							Create(unsigned int nWidth = DEFAULT_SWIDTH,
									   unsigned int nHeight = DEFAULT_SHEIGHT,
									   unsigned int nFrameRate = DEFAULT_FRAMERATE,
									   bool bUseShadow = true,
									   bool bUseFog = true);
	int							Destroy();
	bool						FlagCreate() { return m_bFlagCreate; }

	static CRenderer*			Instance();

	irr::scene::ISceneManager*	SceneManager() { return m_ptrSmgr; }
	irr::IrrlichtDevice*		Device() { return m_ptrDevice; }

	int							AddDefaultCamera();
	int							AddLight(irr::core::vector3df pos, irr::video::SColorf color,
										 float radius);
	irr::scene::ISceneNode*		AddBox(irr::core::vector3df pos,
									   irr::core::vector3df size, 
									   irr::video::SColor color = irr::video::SColor(255, 255, 255, 255));
	irr::scene::ISceneNode*		AddSphere(std::string name, int nId, irr::core::vector3df pos, float radius,
										  irr::video::SColor color = irr::video::SColor(255, 255, 255, 255));
	irr::scene::ISceneNode*		AddText(std::string name, irr::core::vector3df pos, 
										irr::video::SColor color, 
										irr::scene::ISceneNode* ptrParent = NULL);

	irr::gui::IGUIStaticText*	AddStaticText(std::string name, irr::core::rect<irr::s32> position, irr::video::SColor color);

	irr::video::ITexture*		AddTextureFromSColor(irr::video::SColor& color, 
													 irr::c8* name, 
													 irr::core::dimension2d<irr::u32>& size);

	void						DrawCircle(irr::core::vector3df center, irr::f32 radius, irr::u32 vertexcount, 
										  irr::core::vector3df normal, irr::video::SColor color);

	void						SetSelectedNode(irr::scene::ISceneNode* ptrNode); 

	int							GetTime();

	void						Step();

	void						UpdateQuery();
	void						AddSceneNode2Cat(CATEGORY category, irr::scene::ISceneNode* pNode);
	void						FlipVisible(CATEGORY category);
	void						FilterNodes(double minDisplay, int key);
	void						UpdateSentimentBar();
	void						t_ConvertC2W(const char* ptrSrc, wchar_t* ptrDes, int nSize);

private:
	CRenderer(); 
	CRenderer(const CRenderer&);
	CRenderer& operator=(const CRenderer&);

	void						m_SetMat(irr::video::SMaterial& mat, irr::video::SColor color);

	bool						m_bFlagCreate;

	static CRenderer*			m_ptrInstance;

	// frame rate per second
	unsigned int				m_nFrameRate;
	unsigned short				m_minTime;

	irr::IrrlichtDevice*		m_ptrDevice;
	irr::scene::ISceneManager*	m_ptrSmgr;
	irr::video::IVideoDriver*	m_ptrDriver;
	irr::scene::ISceneNode*		m_ptrSelectedNode;
	irr::scene::ISceneNode*		m_ptrPrevNode;
	irr::scene::ICameraSceneNode* m_aCamera[2];
	irr::gui::IGUIEditBox*		m_ptrQueryEdit;

	CRatingGUI*					m_ptrRatingGUI;
	CEmotionBar*				m_ptrEmotionGUI;
	CAvgEmoGraph*				m_ptrAvgEmoGraph;
	SentimentBar*				m_ptrSentimentBar;
	irr::gui::IGUIStaticText*				m_ptrPosSentimentDesc;
	irr::gui::IGUIStaticText*				m_ptrNegSentimentDesc;

	int							m_selectedId;

	std::vector<irr::gui::IGUICheckBox*>	m_ptrCheckBoxList;
	irr::gui::CGUIEditBoxIRB*				m_ptrCodeBox;
	irr::gui::CGUIEditBoxIRB*				m_ptrInfoBox;

	std::unordered_map<irr::scene::ISceneNode*, int>	m_dataList;
	std::vector<irr::scene::ISceneNode*>	m_categoryList[QUERY];

	double							m_minRatingVal;
	double							m_minSentimentVal;

protected:
	void						t_LoadAffect();	
	void						t_Render();
	void						t_UpdateInfo(int nId);
};

#endif					//_RENDERER_H_